
inherits(framework.WorkspaceView);

function Workspace(model, defaultapp, loginclass) {
    
    this.defaultapp = defaultapp;
    this.loginclass = loginclass;
    this.model = model;
    
    window.workspace = this;
    
    base(this,arguments)();
    this.menu.onitemclick.add(this.onMenuitemClick);
    
    
    this.menu.onitemclick.add(function(view,record){
        view.foreach(function(subrecord) {
            subrecord.set("activo", subrecord != record);
        });
    });
    
    
    window.dhtmlHistory.addListener(function(location,data) {
        this.menu.foreach(function(record){
            record.set("activo", record.get("class") != location); 
        });
        
        if(location == "") {
            this.setStyles();
        } else {
            this.loadApplication(location);
        }
        
    }.bind(this));
    
    var hash = window.location.href.split("#",2)[1];
     var parts;
    
    
    
    
    if(!hash)
    {
        parts = [];
        hash = defaultapp;
    } else {
        parts = hash.split("/");
    
        hash = parts.splice(0, 1)[0];
    }

    this.model.getInitialData(hash,parts,
       function(data) {
        this.menu.add(data.menu);
        this.setIsLogin(data.login); 
       
        this.loadApplication(hash, data.home, parts);
        document.body.style.display  = "";
        
        
    }.bind(this));
    
    
}

function test() {
  alert("lala");
}

function loadApplication(id, loadData, clientData) {

    var end = function() {
    	
    	var aux = [];
    	if(clientData) {
    		for(var i = 0; i < clientData.length; i++) {
    			aux.push(clientData[i].toString());
    		}
    	}
    	
    	aux = aux.length > 0 ? ("/" + aux.join("")) : "";
    	
    	
    	
        //Termino
        dhtmlHistory.add(id + aux,null);
        this.page.setStyles();

        klass.model.onlock.add(function(message) {

            this.page.setStyles("process");
            this.loadMessage.setContent(message)
            
        }.bind(this));
        
        klass.model.onunlock.add(function() {
            this.page.setStyles();
            
        }.bind(this));
        
        this.klass = klass;
    }.bind(this);
    
    try {

        
        var klass = id.split(",");
        klass = new (jsworks.resolve(klass[0],klass[1]))(clientData);
        this.page.replace(this.page.getLast(), klass  );
        
        
        klass.model.onerror.add(function(sender, message) {
            dhtmlHistory.add("ERROR");            
            console.error(message);
            this.exception_message.setContent(message);
            this.page.setStyles("exception");
            
        }.bind(this));
        
        if(loadData) {
            klass.loadInitialData(loadData);
        }
        
        if(klass.model.getInitialData && !loadData) {
            this.page.setStyles("loading");
          
            klass.model.getInitialData(function(data) {
                try {
                
                    klass.loadInitialData(data);
                
                    
                    
                
                    end(klass);
                } catch(ex) {
                    console.error(ex.message);
                    this.exception_message.setContent(ex.message);
                    this.page.setStyles("exception");
                }
                
                
                
            }.bind(this), clientData );
            
        } else {
            end(klass);
        }
        
        
    } catch(ex) {
        console.error(ex.message);
        this.exception_message.setContent(ex.message);
        this.page.setStyles("exception");
    }
}

function onMenuitemClick(sender, record) {
    this.loadApplication(record.get("class"), null, record.get("params")); 
}



function setIsLogin(isLogin) {
    this.loginText.setContent(isLogin ? "Cerrar sesión": "Iniciar sesión");
    this.isLogin = isLogin;
}





function onLoginButtonClick() {
    if(!this.isLogin) {
        this.loadApplication(this.loginclass);
    } else {
        this.model.doLogout(function(data) {
            this.loadApplication(this.defaultapp, data.home);
            this.menu.clear();
            this.menu.add(data.menu);
            this.setIsLogin(false);
        }.bind(this));
    }
}




